﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

namespace AIS.SharePoint.AspNetFBA.Code
{
    public enum RibbonType
    {
        CommitRibbon
    }

    public class Ribbon
    {
        /// <summary>
        /// Performs the setup logic to display a ribbon on a popup dialog
        /// </summary>
        /// <param name="page">Current page reference</param>
        /// <param name="ribbonCommandList">Command list to associate with the ribbon</param>
        /// <param name="commandButtonSection">Reference to the command button section of the web page.  Can be NULL</param>
        /// <param name="ribbonToActivate">Ribbon to activate (future expansion)</param>
        public static void ActivateRibbon(System.Web.UI.Page page, List<IRibbonCommand> ribbonCommandList, System.Web.UI.UserControl commandButtonSection, RibbonType ribbonToActivate)
        {
            // NOTE: We are only displaying the commit ribbon on Popups.  In our minds, the ribbon does not has a good look and feel for non-popup pages

            try
            {
                bool IsDialog = Common.IsCurrentContextADialog();

                SPRibbon RibbonInstance = SPRibbon.GetCurrent(page);

                // Ribbon display logic
                // Ribbon is displayed if
                // 1) There is a ribbon
                // 2) We are in a dialog 
                if (RibbonInstance != null && IsDialog)
                {
                    // Enable the ribbon
                    RibbonInstance.Minimized = false;
                    RibbonInstance.CommandUIVisible = true;
                    RibbonInstance.Enabled = true;
                    switch (ribbonToActivate)
                    {
                        case RibbonType.CommitRibbon:
                            RibbonInstance.MakeTabAvailable("AIS.SharePoint.AspNetFAB.Ribbon.Edit");
                            RibbonInstance.InitialTabId = "AIS.SharePoint.AspNetFAB.Ribbon.Edit";
                            break;

                    }

                    // Register required scripts
                    // NOTE: the RibbonGlue.js *must* be loaded last or the ribbon will not initialize
                    ScriptLink.RegisterScriptAfterUI(page, "CUI.js", false, true);
                    ScriptLink.RegisterScriptAfterUI(page, "SP.Runtime.js", false, true);
                    ScriptLink.RegisterScriptAfterUI(page, "SP.js", false, true);
                    ScriptLink.RegisterScriptAfterUI(page, "SP.Ribbon.js", false, true);
                    ScriptLink.RegisterScriptAfterUI(page, "AIS.SharePoint.AspNetFBA/AIS.UI.RibbonGlue.js", false, true);

                    // Wire up ribbon event commands to the ribbon manager
                    SPRibbonScriptManager spRibbonScriptManager = new SPRibbonScriptManager();
                    spRibbonScriptManager.RegisterGetCommandsFunction(page, "getGlobalCommands", ribbonCommandList);
                    spRibbonScriptManager.RegisterCommandEnabledFunction(page, "commandEnabled", ribbonCommandList);
                    spRibbonScriptManager.RegisterHandleCommandFunction(page, "handleCommand", ribbonCommandList);
                }

                // Display the command button section (usually located at the bottom of the form) if any of the following conditions are met
                // 1) No ribbon is present
                // 2) The form is not a popup
                if (commandButtonSection != null)
                {
                    commandButtonSection.Visible = ((RibbonInstance == null) || !Common.IsCurrentContextADialog());
                }
            }
            catch (Exception ex)
            {
                Code.Log.Error(ex, Microsoft.SharePoint.Administration.TraceSeverity.High, Code.Log.Category.Rendering);
                SPUtility.TransferToErrorPage(ex.Message);
            }
        }
    }
}
